package com.fr.data.impl;

import com.fr.base.FCloneable;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary.MV;
import com.fr.data.condition.Condition;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.core.ParameterHelper;
import com.fr.report.core.ScriptUtils;
import com.fr.report.script.Calculator;
import com.fr.util.Utils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DynamicSQLDict extends FormulaDisplayDictionary
{
  private DatabaseConnection databaseConnection;
  private String sqlFormula;
  private transient List kvs;
  private transient Map cachedKVMap;

  public void setDatabaseConnection(DatabaseConnection paramDatabaseConnection)
  {
    this.databaseConnection = paramDatabaseConnection;
  }

  public DatabaseConnection getDatabaseConnection()
  {
    return this.databaseConnection;
  }

  public void setSqlFormula(String paramString)
  {
    this.sqlFormula = paramString;
  }

  public String getSqlFormula()
  {
    return this.sqlFormula;
  }

  public Object get(Object paramObject, Calculator paramCalculator)
  {
    createKVS(paramCalculator);
    return this.cachedKVMap.get(paramObject);
  }

  public Iterator entrys(Calculator paramCalculator)
  {
    createKVS(paramCalculator);
    return new EntryIterator(this, null);
  }

  public void reset()
  {
    this.kvs = null;
    this.cachedKVMap = null;
  }

  private void createKVS(Calculator paramCalculator)
  {
    int i;
    if (this.kvs == null)
    {
      if (paramCalculator == null)
        paramCalculator = Calculator.createCalculator();
      this.kvs = createMVList(new DBTableData(this.databaseConnection, paramCalculator.renderTpl(this.sqlFormula)), paramCalculator);
      this.cachedKVMap = new HashMap(this.kvs.size());
      for (i = 0; i < this.kvs.size(); ++i)
      {
        Dictionary.MV localMV = (Dictionary.MV)this.kvs.get(i);
        if (!(this.cachedKVMap.containsKey(localMV.getModel())))
          this.cachedKVMap.put(localMV.getModel(), localMV.getView());
      }
    }
  }

  public String[] getDependence()
  {
    return ScriptUtils.getDependenceByParameter(ParameterHelper.analyze4Parameters(this.sqlFormula));
  }

  public boolean equlas(Object paramObject)
  {
    return ((paramObject instanceof DynamicSQLDict) && (super.equals(paramObject)) && (ComparatorUtils.equals(((DynamicSQLDict)paramObject).databaseConnection, this.databaseConnection)));
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
      if ("Formula".equals(paramXMLableReader.getTagName()))
      {
        setSqlFormula(paramXMLableReader.getElementValue());
      }
      else if ("SQLDict".equals(paramXMLableReader.getTagName()))
      {
        setKeyColumnIndex(Utils.string2Number(paramXMLableReader.getAttr("keyColumnIndex")).intValue());
        setValueColumnIndex(Utils.string2Number(paramXMLableReader.getAttr("valueColumnIndex")).intValue());
      }
      else
      {
        Object localObject;
        if ("Connection".equals(paramXMLableReader.getTagName()))
        {
          localObject = DataXMLUtils.readXMLConnection(paramXMLableReader);
          if (localObject instanceof DatabaseConnection)
            setDatabaseConnection((DatabaseConnection)localObject);
        }
        else if ("Condition".equals(paramXMLableReader.getTagName()))
        {
          localObject = DataXMLUtils.readCondition(paramXMLableReader);
          setCondition((Condition)localObject);
        }
      }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (StringUtils.isNotBlank(this.sqlFormula))
      paramXMLPrintWriter.startTAG("Formula").textNode(this.sqlFormula).end();
    if (getDatabaseConnection() != null)
      DataXMLUtils.writeXMLConnection(paramXMLPrintWriter, getDatabaseConnection());
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    DynamicSQLDict localDynamicSQLDict = (DynamicSQLDict)super.clone();
    if (this.databaseConnection != null)
      localDynamicSQLDict.databaseConnection = ((DatabaseConnection)this.databaseConnection.clone());
    if (this.condition != null)
      localDynamicSQLDict.condition = ((Condition)this.condition.clone());
    return localDynamicSQLDict;
  }

  static List access$100(DynamicSQLDict paramDynamicSQLDict)
  {
    return paramDynamicSQLDict.kvs;
  }

  private class EntryIterator
    implements Iterator
  {
    private int next;
    private final DynamicSQLDict this$0;

    private EntryIterator()
    {
      this.this$0 = paramDynamicSQLDict;
      this.next = 0;
    }

    public boolean hasNext()
    {
      return (this.next < DynamicSQLDict.access$100(this.this$0).size());
    }

    public Object next()
    {
      Object localObject = DynamicSQLDict.access$100(this.this$0).get(this.next);
      this.next += 1;
      return localObject;
    }

    public void remove()
    {
    }

    EntryIterator(, DynamicSQLDict.1 param1)
    {
      this(paramDynamicSQLDict);
    }
  }
}